const state = {
  cachedViews: [], // 缓存的视图，主要用作keep-alive白名单
  visitedViews: [] // 访问过的路由
}

const mutations = {
  add_cached_view: (state, view) => {
    let name = view.name
    if (!state.cachedViews.includes(name)) {
      if (view.meta && view.meta.isCache) {
        state.cachedViews.push(name)
      }
    }
  },
  add_visited_view: (state, view) => {
    if (state.visitedViews.some(v => v.path === view.path)) return
    let viewObj = Object.assign({}, view, { title: view.meta.title || 'noName' })
    state.visitedViews.push(viewObj)
  },
  delete_cached_view: (state, view) => {
    let index = state.cachedViews.indexOf(view.name)
    if (index !== -1) {
      state.cachedViews.splice(index, 1)
    }
  },
  delete_visited_view: (state, view) => {
    let index = state.visitedViews.findIndex(item => item.path === view.path)
    if (index !== -1) {
      state.visitedViews.splice(index, 1)
    }
  }
}

const actions = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
